package com.fengdi.controller;

import com.fengdi.pojo.Result;
import com.fengdi.pojo.User;
import com.fengdi.service.serviceInterface.UserService;
import com.fengdi.utils.JwtUtil;
import com.fengdi.utils.ThreadLocalUtil;
import com.fengdi.utils.UserRegisterInfoChecker;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @author FengDi
     * @Description 用户注册
     * @param username: 用户名
     * @param password: 密码
     * @return 响应结果
     * */
    @PostMapping("/register")
    public Result<?> userRegister(String username, String password) {
        // 查询用户是否已经存在
        if (userService.findByUsername(username) == null) {
            // 校验长度是否符合要求
            if (UserRegisterInfoChecker.checkStr_5_16(username) && UserRegisterInfoChecker.checkStr_5_16(password)) {
                userService.userRegister(username, password);
                return Result.success();
            } else {
                return Result.error("账号或密码不符合格式要求");
            }
        }
        return Result.error("已存在用户");
    }

    /**
     * @author FengDi
     * @Description 用户登录
     * @param username: 用户名
     * @param password: 密码
     * @return Result
     * */
    @PostMapping("/login")
    public Result<?> userLogin(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        return userService.userLogin(username, password);
    }

    /**
     * @author FengDi
     * @Description 查询用户详细信息
     * @return  用户数据
     * */
    @GetMapping("/userInfo")
    public Result<User> userInfo() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        return this.userService.getUserInfo(claims.get("username").toString());
    }

    /**
     * @author FengDi
     * @Description 更新用户详细数据
     * @param   user: User类型参数，存放更新数据
     * @return  更新结果
     * */
    @PutMapping("/update")
    public Result<?> userUpdate(@RequestBody @Validated User user) {
        return this.userService.update(user);
    }

    /**
     * @author FengDi
     * @Description 更新用户头像
     * @param avatarUrl: 头像路径
     * @return 更新结果
     * */
    @PatchMapping("/updateAvatar")
    public Result<?> userUpdateAvatar(@RequestParam("avatarUrl") @URL String avatarUrl) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        return this.userService.updateUserAvatar(claims.get("username").toString(), avatarUrl);
    }

    /**
     * @author FengDi
     * @Description 修改密码
     * @param   pwdMap: 前端传来的新旧密码键值对
     * @return 修改结果
     * */
    @PatchMapping("/updatePwd")
    public Result<?> userUpdatePwd(@RequestBody @NotNull Map<String, String> pwdMap) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        return this.userService.updateUserPwd(claims.get("username").toString(), pwdMap);
    }

}
